import {
	viteMockServe
} from 'vite-plugin-mock'
import {
	defineConfig,
	loadEnv
} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import {
	ElementPlusResolver
} from 'unplugin-vue-components/resolvers'
import {
	resolve
} from 'path'

// https://vitejs.dev/config/
export default defineConfig(({
	command,
	mode
}) => {
	// 现在就可以用env这个变量点出你在.env文件里面定义的字段了
	const env = loadEnv(mode, process.cwd()) // 获取 .env里面定义是参数
	console.log('env', env, mode, command)
	console.log("🚀 ~ file: vite.config.js:28 ~ command:", env.VITE_USE_MOCK)
	return {
		server: {
			public: 'www.mugechenxing.com',
			host: '0.0.0.0',
			port: '3000',
			open: false, //自动打开 
			base: "./ ", //生产环境路径
			proxy: { // 本地开发环境通过代理实现跨域，生产环境使用 nginx 转发
				// 正则表达式写法
				'^/api': {
					target: env.VITE_APP_BASE_URL, // 后端服务实际地址
					changeOrigin: true, //开启代理
					 rewrite: (path) => path.replace(/^\/api/, '')
				}
			}
		},
		resolve: {
			alias: {
				'@': resolve(__dirname, './src')
			},
		},
		plugins: [
			vue(),
			viteMockServe({
				// default
				supportTs: false,
				logger: true,
				mockPath: './mock/',
				localEnabled: env.VITE_USE_MOCK,
				prodEnabled: false,
			}),
			AutoImport({
				resolvers: [ElementPlusResolver(),
					IconsResolver({
						prefix: 'Icon',
					}),
				],
			}),
			Components({
				resolvers: [ElementPlusResolver(),
					// Auto register icon components
					// 自动注册图标组件
					IconsResolver({
						enabledCollections: ['ep'],
					}),
				],
			}),
			Icons({
				autoInstall: true,
			}),
		],
	}
})